<?php

namespace App\Http\Controllers\Mobile;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Interfaces\GoodsService;
use App\Interfaces\CartService;
use App\Interfaces\OrderService;
use App\Exceptions\Interfaces\CartServiceException;

use App\Services\Allinpay\Services\Response;

use App\Models\Goods;

// use Illuminate\Support\Facades\Auth;
// use Illuminate\Support\Facades\Log;
use InvalidArgumentException;
use Session,Auth,Log;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(CartService $cartService)
    {

        $cart = $cartService->getInfo();

        // dd($cart);

        // return view('mobile.cart.index',['cart'=>$cart]);

        return view('mobile.cart.index',compact('cart'));
    }


    /**
     * 结算购物车
     *
     * @return \Illuminate\Http\Response
     */
    public function settlement(OrderService $orderService, Request $request)
    {
// dd('结算购物车');
        $cartGoods = $request->get('cartGoods');

        Session::put('cartGoods',$cartGoods);

        Session::save();

        if (is_array(Session::get('cartGoods'))){

            return response()->json(['status'=>'success']);
        }else{

            return response()->json(['status'=>'failed','message'=>'参数错误']);
        }

    }//end func settlement



    /**
     * 快速结算
     *
     * @return \Illuminate\Http\Response
     */
    public function fastSettlement(OrderService $orderService, Request $request)
    {

        $cartGoods = $request->get('cartGoods');
        Session::put('fastPay',$cartGoods);

        Session::save();

        if (is_array(Session::get('fastPay'))){

            return response()->json(['status'=>'success']);
        }else{

            return response()->json(['status'=>'failed','message'=>'参数错误']);
        }

    }//end func fastSettlement


    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, CartService $cartService)
    {

        // dd($request->all());

        $return = ['message'=>''];

        $goods_id = $request->goods_id;

        $goods_num = $request->goods_num;

        if(empty($goods_id)){

            $return['message'] = '商品id不能为空';

            return Response()->json($return);
        }//end if()

        if(empty($goods_num)){

            $return['message'] = '商品数量不能为空';

            return Response()->json($return);
        }//end if()

        try{

            $cartService->addGoods($goods_id,$goods_num);
            //$cartInfo = $cartService->getInfo();
            //$return['goods_num'] = $cartInfo['total_number'];

            $cartNum=$cartService->getCartNumber();

            $return['goods_num'] = $cartNum;

        } catch (InvalidArgumentException $e) {

            $return['message'] = $e->getMessage();


        }//end try

        return Response()->json($return);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id, CartService $cartService, Request $request)
    {

        $return = ['status'=>1, 'message'=>'删除成功', 'data'=>''];

        $goods_ids = $request->goods_ids;

        if(empty($goods_ids)){

            $return['message'] = '要删除的商品id数组不能为空';

            return Response()->json($return);
        }else{

            try {

                $cartService->removeGoods($goods_ids);

            } catch (InvalidArgumentException $e) {

                $return['message'] = $e->getMessage();
            }//end try
        }//end if()

        return Response()->json($return);
    }



    /**
     * 获取商品数组中商品总数、总价、总关税等信息
     *
     * @param array $sel_id : 当前更改的商品id
     *
     * @return mixed 1. 总价,总个数,总关税,更改的商品关税  {"total_number":3,"total_price":"2204.77","total_tariff_price":"222.81","tariff_price":199.9}
     */
    public function getGoodsInfo($goods_id, CartService $cartService, Request $request,GoodsService $goodsService){

        $return = ['status'=>1, 'message'=>'', 'data'=>''];

        $cartGoods = $request->get('cartGoods');

        $buy_num = 0;

        foreach($cartGoods as $key=>$value){

            /*if ($goods_id==$v['goods_id']){

                $buy_num = $v['buy_num'];
            }*/

            try {

                // $cartService->updateNumber($goods_id,$buy_num);
                $cartService->updateNumber($value['goods_id'],$value['buy_num']);

            } catch (CartServiceException $e) {

                $goods_name = Goods::where('goods_id', $value['goods_id'])->pluck('goods_name');

                $return['status'] = 0;

                $return['message'] = $goods_name.$e->getMessage();

                return response()->json($return);
            }
        }

        $cart = $cartService->getGoodsInfo($cartGoods);

        return response()->json(['info'=>$cart]);

    }//end func getGoodsInfo


}
